/* es-lint disable */
let fs = require('fs')
let EventProxy = require('eventproxy')
let moment = require('moment')
let crypto = require('./common/crypto.api')
let ErrorHandle = require('./common/error-handle.api')
let Model = require('./model')
const mongoClient = require('./common/mongodb')

// init
let randoms = [], ep = new EventProxy()
for (let i = 0; i < 10; i++) { randoms.push(crypto.RandomNo()) }
let events = [
  'counter_save',
  'client_save',
  'user1_save',
  'user2_save',
  'user3_save',
  'team1_save',
  'team2_save',
  'interest1_save',
  'interest2_save',
  'interest3_save',
  'interest4_save',
  'interest5_save',
  'announcement1_save',
  'announcement2_save',
  'resource1_save',
  'resource2_save',
  'resource3_save',
  'registry1_save',
  'registry2_save',
  'registry_bucket1_save',
  'registry_bucket2_save',
  'vote1_save',
  'vote2_save',
  'vote_bucket1_save',
  'vote_bucket2_save',
  'vote_bucket3_save',
  'vote_bucket4_save',
  'vote_bucket5_save',
  'party1_save',
  'party2_save',
  'party3_save',
  'party4_save',
  'evaluation_bucket1_save',
  'evaluation_bucket2_save',
  'evaluation1_save',
  'evaluation2_save',
  'scholarship1_save',
  'scholarship2_save',
  'dormitory_inspection1_save',
  'dormitory_inspection2_save',
  'dormitory_inspection3_save',
  'dormitory_inspection4_save',
  'dormitory_inspection5_save',
  'dormitory_inspection6_save',
  'discussion1_save',
  'discussion2_save',
  'contrast1_save',
  'contrast_bucket1_save',
  'contrast_bucket2_save',
  'contrast_bucket3_save'
]

let teamCounter = new Model.Counter({ _id: 'teamId', seq: 0 })
let client = new Model.Client({ 
  _id: crypto.Hash('ssas-学生工作管理系统-scholatsysDevGroup'),
  name: '学生工作管理系统',
  company: 'scholatsysDevGroup'
})
let user1 = new Model.User({
  arancia: randoms[0],
  username: '林启龙',
  phone: '15521039666',
  password: crypto.Hash('123456' + '林启龙' + randoms[0]),
  email: 'address1@hotmail.com',
  class: '日语1班',
  cadre: '学习委员',
  qq: '724628901',
  wechat: 'Luciar12',
  interests: ['ACM', '篮球', 'javascript'],
  manager: true,
  dormitory: '西区三栋',
  room: '224',
  bed: '6',
  avatar: fs.readFileSync('./static/favicon.ico'),
  archive: {
    name: '林启龙',
    sex: '男',
    nation: '汉族',
    politics_status: '共青团员',
    examinee_type: '社会在职人员',
    teaching_student: '是',
    senior_high_school: '广东番禺中学',
    university: '华南师范大学',
    campus: '石牌校区',
    school: '计算机学院',
    major: '网络工程',
    others: '',
    school_roll_num: '20142100011',
    indentity_card_num: '440789199008067534',
    birthdate: '1990年8月6日',
    province: '广东省',
    county: '广州天河',
    contact: '13005891334',
    family_information: '双亲健在，小康家庭',
    father: '林宇翔',
    father_contact: '13480421864',
    mother: '许春景',
    mother_contact: '13680772341',
    address: '广东广州天河华南师范大学（石牌校区）A栋101',
    postcode: '529100',
    addressee: '林先生'
  },
  teams: [{
    creator: true,
    management: {
      announcement: true,
      resource: true,
      vote: true,
      registry: true,
      party: true,
      evaluation: true,
      bursary: true,
      dormitory: true,
      contrast: true
    }
  }],
  current_party_stage: 3
})
let user2 = new Model.User({
  arancia: randoms[1],
  username: '李元祥',
  phone: '15521039677',
  password: crypto.Hash('123456' + '李元祥' + randoms[1]),
  email: 'address2@163.com',
  class: '日语2班',
  cadre: '文娱委员',
  qq: '591416880',
  wechat: 'hi_jave',
  interests: ['排球', '游泳', 'ACM'],
  manager: false,
  dormitory: '西区三栋',
  room: '225',
  bed: '4',
  avatar: fs.readFileSync('./static/favicon.ico'),
  archive: {
    name: '李元祥',
    sex: '男',
    nation: '汉族',
    politics_status: '共青团员',
    examinee_type: '应届毕业生',
    teaching_student: '是',
    senior_high_school: '长沙雅礼中学',
    university: '华南师范大学',
    campus: '石牌校区',
    school: '外国语文化学院',
    major: '日语',
    others: '其他备注',
    school_roll_num: '20142100014',
    indentity_card_num: '440789199008069615',
    birthdate: '1991年10月2日',
    province: '湖南省',
    county: '常德永乐',
    contact: '13445891356',
    family_information: '家庭贫困，父母务农',
    father: '李德崇',
    father_contact: '13480422999',
    mother: '陈美芬',
    mother_contact: '13680778979',
    address: '广东广州天河华南师范大学（石牌校区）西三225',
    postcode: '529100',
    addressee: '李哥'
  },
  teams: [{
    creator: false,
    management: {
      announcement: false,
      resource: false,
      vote: false,
      registry: false,
      party: false,
      evaluation: true,
      bursary: false,
      dormitory: false,
      contrast: false
    }
  }],
  current_party_stage: 2
})
let user3 = new Model.User({
  arancia: randoms[2],
  username: '郭明',
  phone: '15521032222',
  password: crypto.Hash('123456' + '郭明' + randoms[2]),
  email: 'address3@gmail.com',
  class: '俄语1班',
  cadre: '文娱委员',
  qq: '591416880',
  wechat: 'hi_jave',
  interests: ['排球', '游泳', '篮球'],
  manager: false,
  dormitory: '西区三栋',
  room: '224',
  bed: '4',
  avatar: fs.readFileSync('./static/favicon.ico'),
  archive: {
    name: '郭明',
    sex: '男',
    nation: '维吾尔族',
    politics_status: '党员',
    examinee_type: '应届毕业生',
    teaching_student: '是',
    senior_high_school: '新疆喀撒尔中学',
    university: '华南师范大学',
    campus: '石牌校区',
    school: '外国语文化学院',
    major: '俄语',
    others: '其他备注',
    school_roll_num: '20142100014',
    indentity_card_num: '440789199008069615',
    birthdate: '1989年6月12日',
    province: '新疆维吾尔自治区',
    county: '卡撒尔',
    contact: '020-6220332',
    family_information: '经济条件良好',
    father: '郭德超',
    father_contact: '13411422999',
    mother: '李美凤',
    mother_contact: '15580778979',
    address: '广东广州天河华南师范大学（石牌校区）西三224',
    postcode: '529100',
    addressee: '帅小子'
  },
  teams: [{
    creator: false,
    management: {
      announcement: false,
      resource: false,
      vote: false,
      registry: false,
      party: true,
      evaluation: false,
      bursary: false,
      dormitory: false,
      contrast: false
    }
  }],
  current_party_stage: 5
})
let team1 = new Model.Team({
  name: '外国语学院2016级',
  years: 4,
  classes: ['日语1班', '日语2班', '俄语1班', '英语国际班'],
  dormitories: [{
    name: '西区三栋',
    rooms: [{
      room: '224',
      star: false
    }, {
      room: '225',
      star: true
    }, {
      room: '226',
      star: false
    }, {
      room: '301'
    }]
  }],
  members: [user1.id, user2.id, user3.id]
})
let team2 = new Model.Team({
  name: '计算机学院2015级',
  years: 4,
  classes: ['网络工程', '数据库', '嵌入式', '师范班'],
  dormitories: [{
    name: '东区6栋',
    rooms: [{
      room: '713',
      star: true
    }, {
      room: '716'
    }, {
      room: '625'
    }, {
      room: '624'
    }]
  }]
})
let interest1 = new Model.Interest({
  name: '篮球',
  team: team1.id,
  attached: [{
    user: user1.id,
    power: 6
  }, {
    user: user3.id,
    power: 11
  }]
})
let interest2 = new Model.Interest({
  name: 'ACM',
  team: team1.id,
  attached: [{
    user: user1.id,
    power: 24
  }, {
    user: user2.id,
    power: 5
  }]
})
let interest3 = new Model.Interest({
  name: 'javascript',
  team: team1.id,
  attached: [{
    user: user1.id,
    power: 191
  }]
})
let interest4 = new Model.Interest({
  name: '排球',
  team: team1.id,
  attached: [{
    user: user2.id,
    power: 44
  }, {
    user: user3.id,
    power: 19
  }]
})
let interest5 = new Model.Interest({
  name: '游泳',
  team: team1.id,
  attached: [{
    user: user2.id,
    power: 33
  }, {
    user: user3.id,
    power: 14
  }]
})
let announcement1 = new Model.Announcement({
  icon: fs.readFileSync('./static/favicon.ico'),
  title: '广东省\'品高云高校杯\'软件作品参赛通知',
  summary: '有意参赛该项比赛的同学，请于9月15日前报名；9月25日提交作品',
  content: '全院本科学生：\n' +
  '        根据第26届广东省“品高云高校杯”软件作品设计竞赛组委会的通知，现就推荐我院本科学生作品参加该项比赛的相关工作通知如下：\n' +
  '        一、作品提交的方式与时间：请在2017年10月9日上午8:30-9:00将参赛作品的纸质版本、电子版本（光盘）一起提交到学院123办公室陈老师处；\n' +
  '        二、作品要求与答辩要求：请查看竞赛组委会信息发布平台（http://www.scholat.com/team/gaoxiaobei26）。\n' +
  '        如对本项工作有任何疑问，请直接联系学院陈老师，联系电话：13903013835。',
  read_times: 111,
  author: user1.id,
  team: team1.id
})
let announcement2 = new Model.Announcement({
  icon: fs.readFileSync('./static/favicon.ico'),
  title: '石牌校区紧急停水通知',
  summary: '有意参赛该项比赛的同学，请于9月15日前报名；9月25日提交作品',
  content: '石牌校区各单位、各住户：\n' +
  '\n' +
  '        因石牌主供水管破裂漏水，定于2017年12月19日（星期二）上午9：00进行停水开挖抢修，请石牌校区各单位、住户做好储水准备，中心将尽快修复并恢复供水。\n' +
  '\n' +
  '        造成不便之处，敬请谅解。',
  read_times: 23,
  author: user1.id,
  team: team1.id
})
let resource1 = new Model.Resource({
  name: '2014SCNUACM新生赛获奖名单.xls',
  kb_size: 1522,
  ext: 'xls',
  upload: user1.id,
  team: team1.id
})
let resource2 = new Model.Resource({
  name: '毕业生协议签订指南.pdf',
  kb_size: 1888,
  ext: 'pdf',
  upload: user1.id,
  team: team1.id
})
let resource3 = new Model.Resource({
  name: '关于发放申请2017年度奖学金工作指示.doc',
  kb_size: 1522,
  ext: 'doc',
  upload: user1.id,
  team: team1.id
})
let registry1 = new Model.Registry({
  team: team1.id,
  title: '计算机学院参赛登记表',
  summary: ' 参赛报名须知\n' +
  '\n' +
  ' 校外举行的比赛，通常有多种报名渠道，但报名之后，请在本处进行登记。\n' +
  ' 如对本项工作有任何疑问，请联系学院团委科调部，或学院陈国球老师。',
  problems: [
    {
      type: 3,
      title: '您所报名的赛事'
    }, {
      type: 3,
      title: '参赛作品/参赛项目名称'
    }, {
      type: 3,
      title: '参赛队伍名字'
    }, {
      type: 3,
      title: '参赛指导教师姓名（如有)',
      tip: '如暂无参赛教师，需要协助安排，请填写具体要求。'
    }, {
      type: 7,
      title: '参赛负责人信息',
      options: [2, 5, '信息', '姓名', '联系电话', '邮箱', '学号']
    }, {
      type: 7,
      title: '参赛队员信息',
      options: [6, 5, '队员一', '队员二', '队员三', '队员四', '队员五', '信息', '姓名', '联系电话', '邮箱', '学号']
    }, {
      type: 6,
      title: '参赛作品、材料及要求上交的附件'
    }, {
      type: 4,
      title: '参赛作品、如需要学院提供其它支持，请在此列入。'
    }
  ]
})
let registry2 = new Model.Registry({
  team: team1.id,
  title: '计算机学院2017年第九届全国大学生数学竞赛报名表',
  summary: '计算机学院2017年第九届全国大学生数学竞赛报名表\n' +
  '1. 本表格仅限于华南师范大学计算机学院学生参加2017年第九届全国大学生数学竞赛报名所用；\n' +
  '\n' +
  '2. 本报名表格于9月20日23：59开始停止接受报名，请有意报名的同学在此之前完成报名；\n' +
  '\n' +
  '3. 报名的同学，同时需要按通知要求进行缴纳报名费，完成缴费后方算完成报名手续；\n' +
  '\n' +
  '4. 报名及缴费的结果请在9月21-22日在学院网站（点击打开）查询。',
  problems: [
    {
      type: 3,
      require: true,
      title: '姓名'
    }, {
      type: 1,
      require: true,
      title: '性别',
      options: ['男', '女']
    }, {
      type: 3,
      require: true,
      title: '学号'
    }, {
      type: 3,
      require: true,
      title: '邮箱',
    }, {
      type: 3,
      require: true,
      title: '手机'
    }, {
      type: 1,
      require: true,
      title: '年级',
      options: ['2014级本科', '2015级本科', '2016级本科']
    }, {
      type: 5,
      require: true,
      title: '专业',
      options: ['计算机科学与技术（师范）', '计算机科学与技术（软件技术应用）', '计算机科学与技术（数据库方向）', '计算机科学与技术（嵌入式方向）', '计算机科学与技术（网络工程）', '计算机类（未分流）', '软件工程']
    }, {
      type: 3,
      require: true,
      title: '身份证号'
    }
  ]
})
let registry_bucket1 = new Model.RegistryBucket({
  registry: registry1.id,
  registrant: user2.id,
  data: [
    '品高云高校杯软件设计大赛',
    '基于SNS学生自主化学习管理平台研究与构建',
    '青春活力队',
    '陈龙',
    [
      ['林启龙', '15521036856', 'hello@gmail.com', '20142100011']
    ],
    [
      ['李元祥', '15521036856', 'hello@gmail.com', '20142100011'],
      ['陈俊朗', '12221236856', 'world@gmail.com', '20152110033'],
      ['麦超', '13821038856', 'hellox@163.com', '20142100123'],
      ['郑大财', '15121936856', 'kliuwo@mail.com', '20142100222'],
      ['王曦', '13421736856', 'cooljingli@hotmail.com', '20142100045']
    ]
  ]
})
let registry_bucket2 = new Model.RegistryBucket({
  registry: registry1.id,
  registrant: user3.id,
  data: [
    '品高云高校杯软件设计大赛',
    '基于openCV的智能家居',
    '创新大队',
    '李铭立',
    [
      ['邓琪', '15521039981', 'sendtodeng@gmail.com', '20142100001']
    ],
    [
      ['王胜艳', '15555766856', 'kiolming@foxmail.com', '20142100071'],
      ['钟厚明', '13521236856', 'luluen@qq.com', '20152110013']
    ]
  ]
})
let vote1 = new Model.Vote({
  team: team1.id,
  title: '7月班游地点选择',
  type: 1,
  options: ['韶关', '广州', '上海', '武汉', '深圳']
})
let vote2 = new Model.Vote({
  team: team1.id,
  title: '签到时间意愿选择',
  type: 2,
  options: ['上午9点', '上午10点', '上午11点', '下午14点', '下午17点'],
  secret: true
})
let vote_bucket1 = new Model.VoteBucket({
  vote: vote1.id,
  voter: user2.id,
  data: ['韶关']
})
let vote_bucket2 = new Model.VoteBucket({
  vote: vote1.id,
  voter: user3.id,
  data: ['广州']
})
let vote_bucket3 = new Model.VoteBucket({
  vote: vote2.id,
  voter: user2.id,
  data: ['上午9点', '上午10点']
})
let vote_bucket4 = new Model.VoteBucket({
  vote: vote2.id,
  voter: user3.id,
  data: ['上午10点', '上午11点']
})
let vote_bucket5 = new Model.VoteBucket({
  vote: vote2.id,
  voter: user1.id,
  data: ['上午10点', '上午11点', '下午14点']
})
let party1 = new Model.Party({
  user: user2.id,
  team: team1.id,
  stage: 1,
  resource: 'a url here/user2.zip',
  pass: true,
  comment: '审核通过，你已经成为入党积极分子'
})
let party2 = new Model.Party({
  user: user2.id,
  team: team1.id,
  stage: 2,
  resource: 'a url here/user2.zip',
  pass: true,
  comment: '审核通过，你已经成为入党发展对象'
})
let party3 = new Model.Party({
  user: user2.id,
  team: team1.id,
  stage: 3,
  resource: 'a url here/user2.zip',
  pass: false,
  comment: '审核不通过，缺交了思想记录一份'
})
let party4 = new Model.Party({
  user: user3.id,
  team: team1.id,
  stage: 1,
  resource: 'a url here/user2.zip',
  pass: false,
  comment: '审核通过，你已经成为入党发展对象'
})
let evaluation_bucket1 = new Model.RegistryBucket({
  user: user2.id,
  items: [
    {
      type: 2,
      explain: '全国英语能力竞赛二等奖',
      score: 8,
      status: 1
    }, {
      type: 1,
      explain: '广东省优秀共青团员',
      score: 3,
      status: 2
    }, {
      type: 2,
      explain: '全国程序设计大赛优胜奖',
      score: 3,
      status: 3
    }, {
      type: 4,
      explain: '诗歌朗诵大赛一等奖',
      score: 5,
      status: 5
    }, {
      type: 5,
      explain: '学年平均绩点',
      score: 3.88,
      status: 2
    }, {
      type: 6,
      explain: '民主测评分数',
      score: 4,
      status: 2
    }
  ]
})
let evaluation_bucket2 = new Model.RegistryBucket({
  user: user3.id,
  items: [
    {
      type: 1,
      explain: '全国英语能力竞赛特等奖',
      score: 10,
      status: 4
    }, {
      type: 3,
      explain: '挑战杯创新创业大赛一等奖',
      score: 3,
      status: 3
    }, {
      type: 2,
      explain: '全国程序设计大赛三等奖',
      score: 3,
      status: 3
    }, {
      type: 5,
      explain: '学年平均绩点',
      score: 4.11,
      status: 2
    }, {
      type: 6,
      explain: '民主测评分数',
      score: 4.6,
      status: 2
    }
  ]
})
let evaluation1 = new Model.Evaluation({
  name: '2017年度外国语学院奖学金评选',
  stage: 1,
  scholarships: [
    {
      name: '校级一等奖学金',
      applicants: [user1.id]
    }, {
      name: '校级二等奖学金',
      applicants: [user2.id]
    }, {
      name: '校级三等奖学金',
      applicants: [user3.id]
    }
  ],
  buckets: [evaluation_bucket1.id, evaluation_bucket2.id]
})
let evaluation2 = new Model.Evaluation({
  name: '2016年度外国语学院奖学金评选',
  stage: 1,
  scholarships: [
    {
      name: '宋美龄奖学金',
      applicants: [user2.id]
    }, {
      name: '校级一等奖学金',
      applicants: []
    }, {
      name: '校级二等奖学金',
      applicants: [user3.id, user2.id]
    }, {
      name: '校级三等奖学金',
      applicants: []
    }
  ]
})
let scholarship1 = new Model.Scholarship({
  name: '高德公益爱心奖学金',
  deadline: moment('2017-11-22').toDate(),
  applicants: [
    {
      user: user2.id,
      status: 1
    }, {
      user: user3.id,
      status: 2
    }
  ]
})
let scholarship2 = new Model.Scholarship({
  name: '宋美龄国家助学奖学金',
  deadline: moment('2018-01-22').toDate(),
  applicants: [
    {
      user: user2.id,
      status: 2
    }, {
      user: user3.id,
      status: 1
    }
  ]
})
let dormitory_inspection1 = new Model.DormitoryInspection({
  team: team1.id,
  date: moment('2017-10-11').toDate(),
  type: 1,
  judge: user2.id,
  violation: '3号床位上没有摆放整齐',
  score: 7.5
})
let dormitory_inspection2 = new Model.DormitoryInspection({
  team: team1.id,
  date: moment('2017-10-19').toDate(),
  type: 1,
  judge: user2.id,
  violation: '行李箱摆放不整齐',
  score: 6
})
let dormitory_inspection3 = new Model.DormitoryInspection({
  team: team1.id,
  date: moment('2017-10-19').toDate(),
  type: 1,
  judge: user2.id,
  violation: '漱口杯摆放不对',
  score: 9
})
let dormitory_inspection4 = new Model.DormitoryInspection({
  team: team1.id,
  date: moment('2017-09-22').toDate(),
  type: 2,
  judge: user3.id,
  violation: '升旗仪式迟到',
  score: 6
})
let dormitory_inspection5 = new Model.DormitoryInspection({
  team: team1.id,
  date: moment('2017-10-19').toDate(),
  type: 2,
  judge: user2.id,
  violation: '思修课堂纪律不好',
  score: 5.5
})
let dormitory_inspection6 = new Model.DormitoryInspection({
  team: team1.id,
  date: moment('2017-12-13').toDate(),
  type: 2,
  judge: user2.id,
  violation: '未依时提交纪律汇报',
  score: 5
})
let discussion1 = new Model.Discussion({
  team: team1.id,
  originator: user2.id,
  title: '什么是图灵机的停机问题？',
  description: '有哪位同学可以用通俗的语言解释一下停机问题',
  labels: [interest3.id],
  answers: [
    {
      replier: user2.id,
      content: '指判断任意一个程序是否能在有限的时间之内结束运行的问题。图灵机停机问题是不可判定的，意思即是不存在一个图灵机能够判定任意图灵机对于任意输入是否停机。',
      thumb: 2
    }, {
      replier: user3.id,
      content: '为了证明这个问题的不可判定性，我们将基于莱斯定理(Rice’s Theorem)将其规约成另外的问题来判定某个图灵机是否能在给定的输入上，在有限步数内给出确定的是或者不是的答案。 。图灵机停机问题是不可判定的，意思即是不存在一个图灵机能够判定任意图灵机对于任意输入是否停机。',
      thumb: 0
    }, {
      replier: user1.id,
      content: '假设图灵机停机问题是可判定的，即存在一个图灵机HM能够判断任意图灵机M在给定输入I的情况下是否可停机。假设M在输入I可时可停机，则HM输出yes，反之输出no。然而图灵机M本身也是字符串的描述，因此它也可以作为自身的输入。故HM应该可以判定当将M程序本身作为M的输入时，M是否会停机。'
    }
  ]
})
let discussion2 = new Model.Discussion({
  team: team1.id,
  originator: user2.id,
  title: '理解和使用 JavaScript 中的回调函数？',
  description: '有哪位同学可以用通俗的语言解释js回调问题',
  labels: [interest3.id],
  answers: [
    {
      replier: user3.id,
      content: '回调函数来自一种著名的编程范式——函数式编程，在基本层面上，函数式编程指定的了函数的参数。函数式编程虽然现在的使用范围变小了，但它一直被“专业的聪明的”程序员看作是一种难懂的技术，以前是这样，未来也将是如此。',
      thumb: 22
    }, {
      replier: user1.id,
      content: '回调函数被认为是一种高级函数，一种被作为参数传递给另一个函数(在这称作"otherFunction")的高级函数，回调函数会在otherFunction内被调用(或执行)。回调函数的本质是一种模式(一种解决常见问题的模式)，因此回调函数也被称为回调模式。',
      thumb: 0
    }, {
      replier: user1.id,
      content: '正如在前面的例子所看到的，我们传递了一个函数给click方法的形参，click方法将会调用(或执行)我们传递给它的回调函数。这个例子就给出了JavaScript中使用回调函数的一个典型方式，并广泛应用于jQuery中。'
    }
  ]
})
let contrast1 = new Model.Contrast({
  term: 1,
  subjects: [
    {
      interest: interest1.id,
      weight: 3
    }, {
      interest: interest2.id,
      weight: 1
    }, {
      interest: interest3.id,
      weight: 4
    }, {
      interest: interest4.id,
      weight: 2
    }, {
      interest: interest5.id,
      weight: 3
    }
  ]
})
let contrast_bucket1 = new Model.ContrastBucket({
  contrast: contrast1.id,
  user: user1.id,
  data: [88, 90, 91, 92, 78]
})
let contrast_bucket2 = new Model.ContrastBucket({
  contrast: contrast1.id,
  user: user2.id,
  data: [66, 71, 100, 99, 82]
})
let contrast_bucket3 = new Model.ContrastBucket({
  contrast: contrast1.id,
  user: user3.id,
  data: [78, 93, 97, 94, 88]
})

// ref settings
user1.teams[0].team = team1.id
user2.teams[0].team = team1.id
user3.teams[0].team = team1.id

user1.contrast = user2.id
user2.contrast = user1.id
user3.contrast = user1.id

user1.followers = [user2.id, user3.id]
user2.followers = [user1.id]

team1.evaluations = [evaluation1.id, evaluation2.id]
team1.scholarships = [scholarship1.id, scholarship2.id]
team1.contrasts = [contrast1.id]

// save and emit events
teamCounter.save(ep.done('counter_save'))
client.save(ep.done('client_save'))
interest1.save(ep.done('interest1_save'))
interest2.save(ep.done('interest2_save'))
interest3.save(ep.done('interest3_save'))
interest4.save(ep.done('interest4_save'))
interest5.save(ep.done('interest5_save'))
resource1.save(ep.done('resource1_save'))
resource2.save(ep.done('resource2_save'))
resource3.save(ep.done('resource3_save'))
registry1.save(ep.done('registry1_save'))
registry2.save(ep.done('registry2_save'))
registry_bucket1.save(ep.done('registry_bucket1_save'))
registry_bucket2.save(ep.done('registry_bucket2_save'))
vote1.save(ep.done('vote1_save'))
vote2.save(ep.done('vote2_save'))
vote_bucket1.save(ep.done('vote_bucket1_save'))
vote_bucket2.save(ep.done('vote_bucket2_save'))
vote_bucket3.save(ep.done('vote_bucket3_save'))
vote_bucket4.save(ep.done('vote_bucket4_save'))
vote_bucket5.save(ep.done('vote_bucket5_save'))
party1.save(ep.done('party1_save'))
party2.save(ep.done('party2_save'))
party3.save(ep.done('party3_save'))
party4.save(ep.done('party4_save'))
evaluation_bucket1.save(ep.done('evaluation_bucket1_save'))
evaluation_bucket2.save(ep.done('evaluation_bucket2_save'))
evaluation1.save(ep.done('evaluation1_save'))
evaluation2.save(ep.done('evaluation2_save'))
scholarship1.save(ep.done('scholarship1_save'))
scholarship2.save(ep.done('scholarship2_save'))
dormitory_inspection1.save(ep.done('dormitory_inspection1_save'))
dormitory_inspection2.save(ep.done('dormitory_inspection2_save'))
dormitory_inspection3.save(ep.done('dormitory_inspection3_save'))
dormitory_inspection4.save(ep.done('dormitory_inspection4_save'))
dormitory_inspection5.save(ep.done('dormitory_inspection5_save'))
dormitory_inspection6.save(ep.done('dormitory_inspection6_save'))
discussion1.save(ep.done('discussion1_save'))
discussion2.save(ep.done('discussion2_save'))
contrast1.save(ep.done('contrast1_save'))

// event proxy
ep.all('counter_save', function() {
  team1.save(ep.done('team1_save'))
  team2.save(ep.done('team2_save'))
})

ep.all('team1_save', function(team1) {
  announcement1.announcement_tag = team1.announcement_tags[6]
  announcement2.announcement_tag = team1.announcement_tags[4]
  announcement1.save(ep.done('announcement1_save'))
  announcement2.save(ep.done('announcement2_save'))

  // 每个团队默认初始7类通知标签
  // 归档两条通知
  Model.AnnouncementTag
    .findByIdAndUpdate(team1.announcement_tags[6], { $push: { list: announcement1.id } }, ErrorHandle.handleErrAny)
  Model.AnnouncementTag
    .findByIdAndUpdate(team1.announcement_tags[4], { $push: { list: announcement2.id } }, ErrorHandle.handleErrAny)

  // 每个团队默认有三个分组：班干部，评优，党团
  Model.ManagementTag
    .findByIdAndUpdate(team1.management_tags[0], { $push: { members: user1.id } }, ErrorHandle.handleErrAny)
  Model.ManagementTag
    .findByIdAndUpdate(team1.management_tags[1], { $push: { members: user2.id } }, ErrorHandle.handleErrAny)
  Model.ManagementTag
    .findByIdAndUpdate(team1.management_tags[2], { $push: { members: user3.id } }, ErrorHandle.handleErrAny)

  user1.teams[0].management_tags = [team1.management_tags[0]]
  user2.teams[0].management_tags = [team1.management_tags[1]]
  user3.teams[0].management_tags = [team1.management_tags[2]]

  user1.save(ep.done('user1_save'))
  user2.save(ep.done('user2_save'))
  user3.save(ep.done('user3_save'))
})

ep.all('contrast1_save', function() {
  contrast_bucket1.save(ep.done('contrast_bucket1_save'))
  contrast_bucket2.save(ep.done('contrast_bucket2_save'))
  contrast_bucket3.save(ep.done('contrast_bucket3_save'))
})

ep.all(events, function() {
  console.log('Hi,mongodb initialization is successfully done!')
  process.exit(0)
})

ep.fail(function(err) {
  console.log('[ERROR]mongo-init.js运行出错，dev用户权限不足，请手动删除scholatsys数据库')
  console.log(err)
  process.exit(1)
})

